package io.itit.ecp.admin.server.service.impl;

import com.alibaba.fastjson.JSONArray;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import io.itit.ecp.admin.server.entity.*;
import io.itit.ecp.admin.server.mapper.DeviceWarningMapper;
import io.itit.ecp.admin.server.service.IDeviceWarningService;
import io.itit.ecp.admin.server.service.IDeviceWarningThresholdService;
import io.itit.ecp.admin.server.service.IDeviceWarningUserService;
import io.itit.grass.common.exception.GrassException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor
public class DeviceWarningServiceImpl extends JoinServiceImpl<DeviceWarningMapper, DeviceWarningEntity>
        implements IDeviceWarningService {

    private final IDeviceWarningUserService deviceWarningUserService;

    @Resource
    private IDeviceWarningThresholdService deviceWarningThresholdService;

    @Override
    @Transactional
    public boolean saveOrUpdateDeviceWarning(DeviceWarningEntity deviceWarningEntity,
                                             List<String> userIds, String warningJson) {
        boolean result = super.saveOrUpdate(deviceWarningEntity);
        deviceWarningThresholdService.removeByDeviceWarningId(deviceWarningEntity.getId());
        deviceWarningUserService.removeByDeviceWarningId(deviceWarningEntity.getId());
        List<DeviceWarningThresholdEntity> thresholds = JSONArray.parseArray(warningJson, DeviceWarningThresholdEntity.class);
        thresholds.forEach(threshold -> threshold.setDeviceWarningId(deviceWarningEntity.getId()));
        deviceWarningThresholdService.saveBatch(thresholds);
        GrassException.throwEx(CollectionUtils.isEmpty(userIds), "推送目标不能为空");
        List<DeviceWarningUserEntity> warningUsers = userIds.stream().map(userId -> {
            DeviceWarningUserEntity warningUser = new DeviceWarningUserEntity();
            warningUser.setUserId(userId);
            warningUser.setDeviceWarningId(deviceWarningEntity.getId());
            return warningUser;
        }).collect(Collectors.toList());
        deviceWarningUserService.saveBatch(warningUsers);
        return result;
    }

    @Override
    @Transactional
    public boolean removeById(Serializable id) {
        deviceWarningUserService.removeByDeviceWarningId(id);
        deviceWarningThresholdService.removeByDeviceWarningId(id);
        return super.removeById(id);
    }

    @Override
    @Transactional
    public boolean removeBatchByIds(Collection<?> ids, int batchSize) {
        deviceWarningUserService.removeByDeviceWarningIds(ids);
        deviceWarningThresholdService.removeByDeviceWarningIds(ids);
        return super.removeBatchByIds(ids, batchSize);
    }
}
